/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"

#include "引擎配置.h"
#include "ui矢量数据部件.h"
#include "../ui绘制模板.h"

#include "UI/UI.h"



/*static void f_改变选项绘制(C_枚举表* box) {
	S_PropValueEnum& e = *((S_PropValueEnum*)box->m_属性数据.m_自定义属性);

	auto* self = box->m_子部件[box->m_前一个选项];
	box->m_背景绘制.f_setColor(box->m_前一个选项, self->m_颜色A);

	box->m_背景绘制.f_setColor(e.m_当前选项, self->m_颜色C);
	box->m_前一个选项 = e.m_当前选项;

	if (box->m_属性数据.m_UI->m_Update) {
		box->m_属性数据.m_UI->m_Update(box->m_属性数据);
	}
}*/

/*void on_枚举表属性改变(S_Props& prop) {
	C_枚举表* box = dynamic_cast<C_枚举表*>(prop.m_UI->m_部件);
	f_改变选项绘制(box);
}*/



static E_事件是否传递 on_枚举表鼠标进入(C_Widget* self, const S_鼠标& 轮值) {
	C_枚举表* box = dynamic_cast<C_枚举表*>(self->m_父部件);

	switch (box->m_自定义属性.m_Type) {
		case E_值类型::e_Type_UI32Ops: 
		case E_值类型::e_Type_UI32位域枚举: {
			uint32 位域 = f_prop_UI32位域枚举(box->m_自定义属性);
			if ((位域 & (1 << self->m_实例ID)) == false) {
				//box->m_背景绘制.f_setColor(self->m_实例ID, S_UI主题::uic_菜单焦点色);
				self->m_颜色A = S_UI主题::uic_按钮焦点;
			}
			break;
		}
		default: {
			auto& 当前选项 = f_prop_enum(box->m_自定义属性);
			if (self->m_实例ID != 当前选项) {
				//box->m_背景绘制.f_setColor(self->m_实例ID, self->m_颜色B);
			}
		}
	}

	box->m_更新绘制属性 = true;
	return E_事件是否传递::e_事件传递_继续;
}

static E_事件是否传递 on_枚举表鼠标退出(C_Widget* self, const S_鼠标& 轮值) {
	C_枚举表* box = dynamic_cast<C_枚举表*>(self->m_父部件);

	switch (box->m_自定义属性.m_Type) {
		case E_值类型::e_Type_UI32Ops: 
		case E_值类型::e_Type_UI32位域枚举: {
			uint32 位域 = f_prop_UI32位域枚举(box->m_自定义属性);
			if ((位域 & (1 << self->m_实例ID)) == false) {
				//box->m_背景绘制.f_setColor(self->m_实例ID, S_UI主题::uic_按钮);
				self->m_颜色A = S_UI主题::uic_按钮;
			}
			break;
		}
		default: {
			auto& 当前选项 = f_prop_enum(box->m_自定义属性);
			if (self->m_实例ID != 当前选项) {
				//box->m_背景绘制.f_setColor(self->m_实例ID, self->m_颜色A);
			}
		}
	}

	box->m_更新绘制属性 = true;
	return E_事件是否传递::e_事件传递_继续;
}

static E_事件是否传递 f_枚举表_鼠标点击(C_Widget* self, const E_鼠标按钮& 按钮) {
	C_枚举表* box = dynamic_cast<C_枚举表*>(self->m_父部件);
	
	switch (box->m_自定义属性.m_Type) {
		case E_值类型::e_Type_UI32位域枚举: {
			auto ws = self->f_get更新组件();

			uint32& 位域 = f_prop_UI32位域枚举(box->m_自定义属性);
			if (位域 & (1 << self->m_实例ID)) {
				位域 ^= (1 << self->m_实例ID);

				self->m_颜色A = S_UI主题::uic_按钮;
			}
			else {
				位域 |= (1 << self->m_实例ID);

				self->m_颜色A = S_UI主题::uic_焦点色;
			}

			if (box->m_自定义属性.m_UI->m_Update) {
				box->m_自定义属性.m_UI->m_Update(box->m_自定义属性);
			}
			break;
		}
		case E_值类型::e_Type_UI32Ops: {
			f_prop_UI32位域枚举(box->m_自定义属性) = self->m_实例ID;
			if (box->m_自定义属性.m_UI->m_Update) {
				box->m_自定义属性.m_UI->m_Update(box->m_自定义属性);
			}
			break;
		}

		default: {
			auto& 当前选项 = f_prop_enum(box->m_自定义属性);
			//box->m_背景绘制.f_setColor(当前选项, self->m_颜色A);
			self->m_颜色A = S_UI主题::uic_焦点色;

			当前选项 = self->m_实例ID;
			box->m_前一个选项 = 当前选项;
			//box->m_背景绘制.f_setColor(self->m_实例ID, self->m_颜色C);

			if (box->m_自定义属性.m_UI->m_Update) {
				box->m_自定义属性.m_UI->m_Update(box->m_自定义属性);
			}
			f_prop_enum(box->m_自定义属性) = 当前选项;

			if (box->m_自定义属性.m_GPU缓存[0].m_Mem.m_数量) {
				f_buf_I32_at(box->m_自定义属性.m_GPU缓存[0], 0) = 当前选项;
			}
		}
	}

	f_widget_更新绘制(box);
	return E_事件是否传递::e_事件传递_终止;
}


static void on_枚举表布局(C_Widget* self, C_Widget** w, uint32 num) {
	if (!num) return;

	C_枚举表* list = (C_枚举表*)self;
	
	if (list->m_方向 == E_方向::e_横向) {
		f_HBOX布局({}, self->m_Size, w, num);
	}
	else {
		f_VBOX布局({}, self->m_Size, w, num);
	}

	
}

static void on_枚举表绘制(C_Widget* self, S_2D画布* 画布) {
	C_枚举表& box = *dynamic_cast<C_枚举表*>(self);

	box.m_背景 = f_vg_genConvex((*self), *画布, E_填充模式::e_填充面, E_图层混合模式::e_Normal);
	box.m_字符 = f_vg_genString((*self), *画布, E_填充模式::e_填充面, E_图层混合模式::e_Normal, u"雅黑");
	box.m_图标 = f_vg_genIco((*self), *画布, E_图层混合模式::e_Normal, E_填充模式::e_填充面);

}

static void on_枚举表变换(C_Widget* self) {
	C_枚举表& box = *dynamic_cast<C_枚举表*>(self);
	
	auto we = self->f_get更新组件();

	std::vector<S_2DConvexRect> 区域(we.m_Num + 1);
	std::vector<S_GPU内存块*>	形状(we.m_Num + 1);
	std::vector<S_2D颜色包>		颜色(we.m_Num + 1);
	std::vector<vec2>			位置(we.m_Num + 1);

	std::vector<std::u16string> 标题字符;
	std::vector<S_Rect2Df>		标题区域;
	std::vector<S_RGBA8UI>		标题颜色;

	std::vector<std::u16string> 图标名称;
	std::vector<S_RGBA8UI>		图标颜色;
	std::vector<vec2>			图标位置;
	std::vector<vec2>			图标大小;



	S_2DConvexRect rect{};
	rect.size = box.m_Size;
	rect.radius = {S_UI主题::ui_面板圆角半径, S_UI主题::ui_面板圆角半径};

	区域[0] = rect;
	形状[0] = &(self->m_UIctx->m_Ctx.m_几何图形->m_平面圆角矩形);
	颜色[0] = self->m_ColorA;
	位置[0] = {};
	
	mat3X2 Mat;
	
	switch (box.m_自定义属性.m_Type) {
		case E_值类型::e_Type_UI32Ops: 
		case E_值类型::e_Type_UI32位域枚举: {
			const S_Prop枚举项& item = f_prop_位域枚举项(box.m_自定义属性);

			for (uint32 i = 0; i < box.m_自定义属性.m_值元素个数; ++i) {
				区域.emplace_back(S_2DConvexRect{{}, we.m_W[i]->m_Size, {S_UI主题::ui_面板圆角半径, S_UI主题::ui_面板圆角半径}});
				形状.emplace_back(we.m_W[i]->m_图元形状);
				颜色.emplace_back(we.m_W[i]->m_ColorA);
				位置.emplace_back(we.m_W[i]->m_Pos);

				
				float32 icoSize = we.m_W[i]->m_Size.y * 0.8f;
				
				图标名称.emplace_back(item.m_ICO[i]);
				图标颜色.emplace_back(S_UI主题::uic_文字颜色);
				图标位置.emplace_back(we.m_W[i]->m_Pos + we.m_W[i]->m_Size.y * 0.5f);
				图标大小.emplace_back(vec2{icoSize,icoSize});
			}
			break;
		}
		default:{
			auto e = f_prop_enumItem(box.m_自定义属性);
			if(!box.m_自定义属性.m_值元素个数) return;

			for (uint32 i = 0; i < box.m_自定义属性.m_值元素个数; ++i) {
				区域.emplace_back(S_2DConvexRect{{}, we.m_W[i]->m_Size, {S_UI主题::ui_面板圆角半径, S_UI主题::ui_面板圆角半径}});

				Mat.row0 = we.m_W[i]->m_Pos;
				Mat.row1 = we.m_W[i]->m_Size;
				Mat.row2 = { S_UI主题::ui_面板圆角半径, 0 };
				//box->m_背景绘制.f_set单色填充(i, Mat, box->m_颜色A, ws.m_W[i]->m_图元);

				if (box.m_是否绘制标题) {
					//box->m_文本绘制.f_居中绘制文本({ Mat.row0 , Mat.row1 }, S_UI主题::uic_文字颜色, i, e.m_Names[i]);
				}
				else {
					//box->m_文本绘制.f_居中绘制文本({}, S_UI主题::uic_文字颜色, i, L"");
				}


				//auto& ico = e.m_ICO[i];
				Mat.row0 = we.m_W[i]->m_Pos + 1 * 0.5f;
				Mat.row1.x = 1 * 0.8f;
				//box->m_图标绘制.f_set(e.m_ICO[i], i, Mat, S_UI主题::uic_文字颜色);
			}

			uint32 offset = f_prop_enum(box.m_自定义属性);
			//box->m_背景绘制.f_setColor(offset, S_UI主题::uic_菜单焦点色);
			break;
		}
	}
}


C_枚举表::C_枚举表(S_UI渲染环境& ctx, bool 是否展开, E_方向 方向) : C_Widget(&ctx) {
	m_TypeName = "UI_枚举表";
	m_Type = E_WidgetType::t_枚举表;

	m_前一个选项 = 0;
	
	mf_项改变回调 = nullptr;
	mf_项绘制更新 = nullptr;

	mf_布局 = on_枚举表布局;
	mf_绘制 = on_枚举表绘制;
	mf_变换 = on_枚举表变换;

	m_方向 = 方向;
	m_是否绘制图标 = false;
	m_是否绘制标题 = true;
	

	m_颜色A = S_UI主题::uic_按钮;
	m_颜色B = S_UI主题::uic_按钮焦点;
	m_颜色C = S_UI主题::uic_焦点色;
	m_ColorA.m_颜色 = S_UI主题::uic_按钮;
}

C_枚举表::~C_枚举表() {
}


void C_枚举表::f_bind属性(S_Props& prop) {
	C_Widget::f_bind属性(prop);

	auto e = f_get更新组件();
	S_PropValueEnum 枚举 = *((S_PropValueEnum*)prop.m_Value);
	

	if (prop.m_值元素个数 >= e.m_Num) {
		for (uint8 i = e.m_Num; i < prop.m_值元素个数; ++i) {
			C_Widget* w = new C_Widget();
			w->m_实例ID = i;
			w->m_是否延展 = { true, true };
			w->m_间隔宽度 = 2;

			w->mf_鼠标点击 = f_枚举表_鼠标点击;
			w->mf_鼠标进入 = on_枚举表鼠标进入;
			w->mf_鼠标离开 = on_枚举表鼠标退出;

			w->m_ColorA.m_颜色 = S_UI主题::uic_按钮;
			w->m_颜色A = S_UI主题::uic_按钮;
			w->m_颜色B = S_UI主题::uic_按钮焦点;
			w->m_颜色C = S_UI主题::uic_焦点色;
			w->m_图元形状 = &m_UIctx->m_Ctx.m_几何图形->m_平面矩形;

			f_添加子组件(w);
		}
	}
	else {
		f_widget_remove区间子组件(this, prop.m_值元素个数);
	}



	prop.m_UI->m_部件 = this;
	//prop.m_部件 = this;
	e = f_get更新组件();
	if(!e.m_Num) return;

	if (prop.m_值元素个数 == 1) {
		e.m_W[0]->m_图元形状 = &m_UIctx->m_Ctx.m_几何图形->m_平面圆角矩形;
	}
	else {
		if (m_方向 == E_方向::e_纵向) {
			e.m_W[0]->m_图元形状 = &m_UIctx->m_Ctx.m_几何图形->m_平面上圆角矩形;
			e.m_W[e.m_Num - 1]->m_图元形状 = &m_UIctx->m_Ctx.m_几何图形->m_平面下圆角矩形;
		}
		else {
			e.m_W[0]->m_图元形状 = &m_UIctx->m_Ctx.m_几何图形->m_平面左圆角矩形;
			e.m_W[e.m_Num - 1]->m_图元形状 = &m_UIctx->m_Ctx.m_几何图形->m_平面右圆角矩形;
		}
	}

	for (uint32 i = 0; i < prop.m_值元素个数; ++i) {
		switch (prop.m_Type) {
			//case E_值类型::e_Type_UI32位域枚举: {
			//switch (m_Value.m_Type) {
			//	
			//		break;
			//}
			case E_值类型::e_Type_UI8位域枚举:
			case E_值类型::e_Type_UI16位域枚举:
			case E_值类型::e_Type_UI32位域枚举:
			case E_值类型::e_Type_UI64位域枚举:
			case E_值类型::e_Type_UI128位域枚举: {
				if (f_prop_UI32位域枚举(m_自定义属性) & (1 << i)) {
					e.m_W[i]->m_颜色A = S_UI主题::uic_焦点色;
				}
				else {
					e.m_W[i]->m_颜色A = S_UI主题::uic_按钮;
				}
				break;
			}
			case E_值类型::e_Type_UI32Ops: {
				e.m_W[i]->m_颜色A = S_UI主题::uic_按钮;
				break;
			}
			default:
				break;
		}
	}
	
}

void f_ui_关联枚举组件绘制颜色(C_枚举表* self, C_枚举表* 分身) {
	//auto& e = self->m_画布->m_surf[self->m_背景绘制.m_图元.层];
	//f_vg_关联凸边形颜色属性(self->m_画布, 分身->m_画布, self->m_背景绘制.m_图元.层);

	//if (分身->m_背景绘制.m_图元.链接颜色) {
	//
	//}
	//else {
	//	分身->m_背景绘制.m_图元.颜色 = self->m_背景绘制.m_图元.颜色;
	//}
}


C_Widget* f_ui_创建枚举选择框(S_UI渲染环境& ctx, E_方向 方向) {
	C_Widget* w = new C_枚举表(ctx, true, 方向);
	return w;
}

void f_ui_set枚举选择框(C_Widget* self, E_方向 方向) {
	dynamic_cast<C_枚举表*>(self)->m_方向 = 方向;
}

void f_ui_set枚举选择框图标和文字是否绘制(C_Widget* self, bool 是否绘制图标, bool 是否绘制标题) {
	C_枚举表* box = dynamic_cast<C_枚举表*>(self);
	box->m_是否绘制图标 = 是否绘制图标;
	box->m_是否绘制标题 = 是否绘制标题;
}

ubvec2 f_ui_get枚举表当前值(C_Widget* self) {
	C_枚举表* box = dynamic_cast<C_枚举表*>(self);
	return { uint8(f_prop_enum(box->m_自定义属性)), box->m_前一个选项 };
}

uint8 f_ui_get枚举表项ID(C_Widget* self, uint8 子部件ID, bool 排除激活项) {
	C_枚举表* box = dynamic_cast<C_枚举表*>(self);

	if (排除激活项) {
		if (子部件ID == f_prop_enum(box->m_自定义属性)) {
			return 255;
		}
		else if (子部件ID + 1 >= box->m_自定义属性.m_值元素个数) {
			return 254;
		}
	}
	else {
		if (子部件ID + 1 >= box->m_自定义属性.m_值元素个数) {
			return 254;
		}
	}

	return 子部件ID;
}

bool f_ui_get枚举表是否为激活项(C_Widget* self, uint8 项ID) {
	C_枚举表* box = dynamic_cast<C_枚举表*>(self);
	if (f_prop_enum(box->m_自定义属性) == 项ID) return true;
	return false;
}

void f_ui_bind枚举部件属性(C_Widget* self, S_Props& prop) {
	C_枚举表* box = dynamic_cast<C_枚举表*>(self);
	if (box) {
		box->m_自定义属性 = prop;
	}
}

void f_ui_映射枚举组件绘制颜色(C_Widget* self, C_Widget* 分身) {
	auto 主 = dynamic_cast<C_枚举表*>(self);
	auto 副 = dynamic_cast<C_枚举表*>(分身);
	assert(主);
	assert(副);

	f_widget_关联组件(self, 分身);
	f_ui_关联枚举组件绘制颜色(主, 副);

}


void f_ui_set枚举布局方向(C_Widget* self, E_方向 方向) {
	C_枚举表& box = *dynamic_cast<C_枚举表*>(self);

	if (box.m_子组件.size() >= 2) {
		if (box.m_方向 == E_方向::e_纵向) {
			(box.m_子组件.front())->m_图元形状 = &box.m_UIctx->m_Ctx.m_几何图形->m_平面上圆角矩形;
			(box.m_子组件.back())->m_图元形状 = &box.m_UIctx->m_Ctx.m_几何图形->m_平面下圆角矩形;
		}
		else {
			(box.m_子组件.front())->m_图元形状 = &box.m_UIctx->m_Ctx.m_几何图形->m_平面左圆角矩形;
			(box.m_子组件.back())->m_图元形状 = &box.m_UIctx->m_Ctx.m_几何图形->m_平面右圆角矩形;
		}
	}
	box.m_方向 = 方向;
}









